from flask import Blueprint, request
from utilsapp.mysql_con import db_pagelist, db_fetchone, db_delete, db_update,db_insert ,db_fetchall
from utilsapp.common import assign_value, parsing_data_id,get_operate
import json
bluePrint = Blueprint('algorithm_group', __name__)
tableName = 'algorithm_group'


# 分页查询
@bluePrint.route('/getlist', methods=["post"])
def getlist():
    a = request.json
    scope_id = assign_value(a, "scope_id", 0)
    scoped = parsing_data_id(scope_id, request)
    name = assign_value(a, "name")
    query = ""
    # if scoped != "":
        # query += f" and {scoped}"
    if name != "":
        query += f" and name like '%{name}%'"
    sql = f"select * from {tableName} where 1=1 {query} ORDER BY id DESC"

    res = db_pagelist(sql, a['page'], a['size'])
    return res



@bluePrint.route('/add', methods=['post'])
def add():
    return db_insert(tableName, request.json)

@bluePrint.route('/edit', methods=["post"])
def edit():
    id = assign_value(request.json, "id")
    keys_not_add = ['id']
    addModel = {key: request.json[key]
                    for key in request.json if key not in keys_not_add}
    return db_update(tableName,addModel,f"where id =%s", {"id": id})

@bluePrint.route('delete', methods=['post'])
def delete():
    id = request.json['id']
    return db_delete(tableName, f"where id = %s", {'mid': id})

# @bluePrint.route("/status",methods=['post'])
# def status1():
#     a = request.json
#     id = assign_value(a,"id")
#     is_enable = assign_value(a,"is_enable")
#     return db_update(tableName,{"is_enable",is_enable}, f'WHERE id = %s',{'id': id})